package cn.openea.perpartner.meily.web.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.openea.perpartner.common.core.utils.poi.ExcelUtil;
import cn.openea.perpartner.common.core.web.controller.BaseController;
import cn.openea.perpartner.common.core.web.domain.AjaxResult;
import cn.openea.perpartner.common.core.web.page.TableDataInfo;
import cn.openea.perpartner.meily.util.UserContext;
import cn.openea.perpartner.meily.web.entity.Job;
import cn.openea.perpartner.meily.web.entity.dto.SearchDto;
import cn.openea.perpartner.meily.web.entity.vo.JobVo;
import cn.openea.perpartner.meily.web.service.IJobService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;


/**
 * @author Weird077
 * @description TODO
 * @date 2023-01-06 15:28
 */
@Api(tags = "招聘管理")
@RestController
@RequestMapping("/job")
public class JobController extends BaseController {
    @Autowired
    private IJobService jobService;
    @ApiOperation(value = "分页查询")
    @PostMapping("/list")
    public TableDataInfo list(@RequestBody JobVo vo){
        startPage();
        List<Job> list = jobService.selectList(vo);
        return getDataTable(list);
    }
    @ApiOperation(value = "分页查询-小程序")
    @PostMapping("/applet/list")
    public TableDataInfo queryAll(@RequestBody JobVo vo){
        startPage();
        List<Job> list = jobService.queryAll(vo);
        return getDataTable(list);
    }
    @ApiOperation(value = "第一页")
    @PostMapping("/top/list")
    public TableDataInfo topList(){
        List<Job> list = jobService.topList();
        return getDataTable(list);
    }
    @ApiOperation(value = "职位详情")
    @GetMapping("/get/{id}")
    public AjaxResult getById(@PathVariable("id") Long id){
        return success(jobService.getById(id));
    }
    @ApiOperation(value = "立即沟通")
    @GetMapping("/get/contact/{id}")
    public AjaxResult getContactById(@PathVariable("id") Long id){
        return success(jobService.getContactById(id));
    }
    @ApiOperation(value = "新增职位")
    @PostMapping("/save")
    public AjaxResult save(@RequestBody Job job){
        return toAjax(jobService.insert(job));
    }
    @ApiOperation(value = "复制职位")
    @GetMapping("/copy/{id}")
    public AjaxResult copy(@PathVariable("id") Long id){
        return toAjax(jobService.copy(id));
    }
    @ApiOperation(value = "编辑职位")
    @PostMapping("/edit")
    public AjaxResult edit(@RequestBody Job job){
        job.setUpdateUser(UserContext.getUserId());
        return toAjax(jobService.updateById(job));
    }
    @ApiOperation(value = "删除职位")
    @PostMapping("/delete")
    public AjaxResult delete(@RequestBody List<Long> idList){
        return toAjax(jobService.delete(idList));
    }
    @ApiOperation(value = "大家都在搜")
    @GetMapping("/get/hot")
    public AjaxResult selectHotJob(){
        Job job = jobService.selectHotJob();
        return BeanUtil.isNotEmpty(job)?success(job.getJobName()):error("暂无数据");
    }
    @ApiOperation("置顶职位")
    @GetMapping("/top/{jobId}")
    public AjaxResult top(@PathVariable Long jobId) {

        return toAjax(this.jobService.top(jobId));
    }
    @ApiOperation("取消置顶")
    @GetMapping("/top/cancel/{jobId}")
    public AjaxResult topCancel(@PathVariable Long jobId) {

        return toAjax(this.jobService.topCancel(jobId));
    }
    @ApiOperation("停止招聘")
    @GetMapping("/stop/hire/{jobId}")
    public AjaxResult stopHire(@PathVariable Long jobId) {

        return toAjax(this.jobService.stopHire(jobId));
    }
    @ApiOperation("重新发布")
    @GetMapping("/republish/{jobId}")
    public AjaxResult republish(@PathVariable Long jobId) {

        return toAjax(this.jobService.republish(jobId));
    }

    @ApiOperation("导入Excel")
    @PostMapping("/importData")
    @ResponseBody
    public AjaxResult importData(MultipartFile file) throws Exception
    {
        ExcelUtil<Job> util = new ExcelUtil<Job>(Job.class);
        List<Job> jobList = util.importExcel(file.getInputStream());
        String message = jobService.importData(jobList);
        return AjaxResult.success(message);
    }
}
